﻿#region << 文 件 说 明 >>
/*----------------------------------------------------------------
// 文件名称：MainViewModel
// 创 建 者：杨程
// 创建时间：2021/9/1 11:04:59
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//		
//
//----------------------------------------------------------------*/
#endregion

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using V.CodeGenerator.CreateCodes.Model;
using V.CodeGenerator.Model;
using V.CodeGenerator.ViewModel.Model;
using Vampirewal.Core.Interface;
using Vampirewal.Core.Models;
using Vampirewal.Core.IoC;
using Vampirewal.Core.SimpleMVVM;
using Vampirewal.Core.WpfTheme.WindowStyle;

namespace V.CodeGenerator.ViewModel
{
    [VampirewalIoCRegister(ViewModelKey.MainViewModel, RegisterType.ViewModel)]
    public class MainViewModel : BillVM<ProjectPoco>
    {
        
        private IDialogMessage Dialog;
        public MainViewModel(IAppConfig appConfig,IDialogMessage dialog,IDataContext dc):base(dc,appConfig)
        {
            //构造函数
            Dialog = dialog;

            Title = Config.AppChineseName;

            
        }

        public override void InitData()
        {            
            PageModel = new MainModel();
        }

        public override void PassData(object obj)
        {
            SetEntity(obj as ProjectPoco);

            GetView(ViewKey.Configuration_ProjectBaseInfo);
        }

        bool IsBack = false;
        public override object GetResult()
        {
            
            return IsBack;
        }

        public override void MessengerRegister()
        {

        }

        #region 属性
        
        public MainModel PageModel { get; set; }

        
        private FrameworkElement _UcView;

        public FrameworkElement UcView { get=>_UcView; set { _UcView = value;DoNotify(); } }

        private Dictionary<string, FrameworkElement> UcViewDic { get; set; } = new Dictionary<string, FrameworkElement>();
        #endregion

        #region 公共方法

        #endregion

        #region 私有方法

        private void LoadProjectTree()
        {
            PageModel.projectModelDtls.Clear();
            
            //查找父级第1个
            var first= DC.Client.Queryable<ProjectModelDtl>()
                       //.AsQueryable()
                       //.Include(x => x.project)
                       .First(f => f.ParentId == "00000000-0000-0000-0000-000000000000"&&f.projectId==Entity.BillId);

            ClearList(first);
            //ProjectModelDtl first = new ProjectModelDtl()
            //{
            //    ID = Guid.Empty.ToString(),
            //    DtlName=Entity.ProjectName,
            //    projectId=Entity.ID,
            //    project=Entity
            //};

            PageModel.projectModelDtls.Add(first);

            GetChilds(first);
        }

        private void GetChilds(ProjectModelDtl Father)
        {
            var currentList= DC.Client.Queryable<ProjectModelDtl>().Where(f => f.ParentId == Father.DtlId&& f.projectId== Father.projectId).ToList();
            Father.Childs=new ObservableCollection<ProjectModelDtl>(currentList);
            foreach (var item in currentList)
            {
                ProjectModelDtl dtl = new ProjectModelDtl()
                {
                    DtlId = item.DtlId,
                    DtlName = item.DtlName,
                    projectId = Entity.BillId,
                    //project = Entity
                };

                GetChilds(item);
                //PageModel.projectModelDtls.Add(item);
            }

        }

        private void ClearList(ProjectModelDtl TopFather)
        {
            if (TopFather.Childs.Count>0)
            {
                foreach (var item in TopFather.Childs)
                {
                    ClearList(item);
                }
            }
            TopFather.Childs.Clear();

        }

        private void GetView(string ViewName)
        {
            if (UcViewDic.ContainsKey(ViewName))
            {
                UcView = UcViewDic[ViewName];
            }
            else
            {
                var view = VampirewalIoC.GetInstance().GetInstance<FrameworkElement>( ViewName);
                var vm = view.DataContext as ViewModelBase;
                vm.View = this.View;
                vm.PassData(Entity);
                UcViewDic.Add(ViewName, view);

                UcView = view;
            }
        }
        #endregion

        #region 命令
        /// <summary>
        /// 返还首页命令
        /// </summary>
        public RelayCommand ReturnUpWindowCommand => new RelayCommand(() => 
        {
            IsBack = true;
            //WindowsManager.RegisterWindow(((Window)View));
            ((Window)View).Close();
        });

        /// <summary>
        /// 添加域命令
        /// </summary>
        public RelayCommand<ProjectModelDtl> AddmoduleCommand => new RelayCommand<ProjectModelDtl>((p) => 
        {
            //ProjectModelDtl dtl = new ProjectModelDtl()
            //{
            //    DtlName="test",
            //    projectDtlType=ProjectDtlType.module,
            //    projectId=p.projectId,
            //    //project = p.project,
            //    ParentId =p.ID,
            //    //Parent=p,
            //    CreateTime=DateTime.Now,
            //    CanDelete=true,
            //    ProjectDtlEditType= ProjectDtlEditType.CanEdit
            //};

            //p.Childs.Add(dtl);

            //DC.AddEntity(dtl);

            //ProjectModelDtl ViewFolder = new ProjectModelDtl()
            //{
            //    projectId = p.projectId,
            //    ParentId = dtl.ID,
            //    DtlName = "页面",
            //    projectDtlType = ProjectDtlType.ViewFolder,
            //    IsDetail = false,
            //    SortNum = 1,
            //    ProjectDtlEditType = ProjectDtlEditType.CanShow,
            //    CanDelete = false
            //};

            //DC.AddEntity(ViewFolder);

            //ProjectModelDtl ModelFolder = new ProjectModelDtl()
            //{
            //    projectId = p.projectId,
            //    ParentId = dtl.ID,
            //    DtlName = "模型",
            //    projectDtlType = ProjectDtlType.ModelFolder,
            //    IsDetail = false,
            //    SortNum = 2,
            //    ProjectDtlEditType = ProjectDtlEditType.CanShow,
            //    CanDelete = false

            //};
            //DC.AddEntity(ModelFolder);

            //dtl.Childs.Add(ViewFolder);
            //dtl.Childs.Add(ModelFolder);

            //DC.SaveChanges();

            //LoadProjectTree();
        });

        public RelayCommand<ProjectModelDtl> AddViewCommand => new RelayCommand<ProjectModelDtl>((p) => 
        {
            //if (p!=null&&p.projectDtlType==ProjectDtlType.ViewFolder)
            //{
            //    ProjectModelDtl ViewPage = new ProjectModelDtl()
            //    {
            //        projectId = p.projectId,
            //        ParentId = p.ID,
            //        DtlName = "空白页面",
            //        projectDtlType = ProjectDtlType.View,
            //        IsDetail = true,
            //        SortNum = 2,
            //        ProjectDtlEditType = ProjectDtlEditType.CanEdit,
            //        CanDelete = true
                    
            //    };
            //    DC.AddEntity(ViewPage);
            //    DC.SaveChanges();
            //    p.Childs.Add(ViewPage);

            //    LoadProjectTree();
            //}
        });

        /// <summary>
        /// 添加模型命令
        /// </summary>
        public RelayCommand<ProjectModelDtl> AddModelCommand => new RelayCommand<ProjectModelDtl>((p) => 
        {
            //if (p != null && p.projectDtlType == ProjectDtlType.ModelFolder)
            //{
            //    ProjectModelDtl Model = new ProjectModelDtl()
            //    {
            //        projectId = p.projectId,
            //        ParentId = p.ID,
            //        DtlName = "空白模型",
            //        projectDtlType = ProjectDtlType.Model,
            //        IsDetail = true,
            //        SortNum = 2,
            //        ProjectDtlEditType = ProjectDtlEditType.CanEdit,
            //        CanDelete = true

            //    };
            //    DC.AddEntity(Model);
            //    DC.SaveChanges();
            //    p.Childs.Add(Model);

            //    LoadProjectTree();
            //}
        });


        #region 删除明细的功能
        /// <summary>
        /// 需要删除的PorjectDtl
        /// </summary>
        private List<ProjectModelDtl> NeedDeleteDtlList { get; set; } = new List<ProjectModelDtl>();
        /// <summary>
        /// 删除ProjectModelDtl命令
        /// </summary>
        public RelayCommand<ProjectModelDtl> DeleteProjectDtlCommand => new RelayCommand<ProjectModelDtl>((p) =>
        {
            if (p != null && p.CanDelete)
            {
                NeedDeleteDtlList.Clear();
                GetNeedDeleteProjectDtl(p);

                foreach (var item in NeedDeleteDtlList)
                {
                    DC.DeleteEntity(item);
                }

                int DeleteCount = NeedDeleteDtlList.Count;

                DC.DeleteEntityList(NeedDeleteDtlList);

                Dialog.ShowPopupWindow($"已删除{DeleteCount}条工程明细！", (Window)View, MessageType.Successful);

                LoadProjectTree();
            }
        });

        /// <summary>
        /// 获取将要删除的明细
        /// </summary>
        /// <param name="Father"></param>
        private void GetNeedDeleteProjectDtl(ProjectModelDtl Father)
        {
            NeedDeleteDtlList.Add(Father);

            foreach (var item in Father.Childs)
            {
                GetNeedDeleteProjectDtl(item);
            }
        }
        #endregion

        /// <summary>
        /// 树形菜单选择后事件
        /// </summary>
        public RelayCommand<ProjectModelDtl> SelectTreeItemsCommand => new RelayCommand<ProjectModelDtl>((p) => 
        {
            if (p.IsDetail)
            {
                //if (p.projectDtlType== ProjectDtlType.Model)
                //{
                //    Messenger.Default.Send("GetModelEditEntity", p);
                //}
            }

        });


        public RelayCommand<Window> WindowLoadedCommand => new RelayCommand<Window>((w) => 
        {
            //WindowsManager.RegisterWindow(w);
        });

        /// <summary>
        /// 选择菜单命令
        /// </summary>
        public RelayCommand<string> SelectMenuCommand => new RelayCommand<string>((s) =>
        {
            GetView(s);
        });
        #endregion
    }

    public static class tuokan
    {
        public static ObservableCollection<T> TreeAddChild<T>(this ObservableCollection<T> source,T t) where T : TreeDetailBaseModel<T>
        {
            try
            {
                source.First(f => f.DtlId == t.ParentId).Childs.Add(t);
                return source;
            }
            catch 
            {

                return source;
            }
             

            
        }
    }
}
